import service from '../../utils/request';
import {setToken} from '../../utils/auth'
const state = {
    token: ''
}

const mutations = {
    SET_TOKEN(state, token) {
        state.token = token;
    }
}

const actions = {
    login({commit}, userInfo) {
        const {username, password} = userInfo;
        return new Promise((resolve, reject) => {
            service.post('/login', {username: username.trim(), password: password}).then(response => {
                commit('SET_TOKEN', response.data.token);
                setToken(response.data.token);
                resolve();
            }).catch(error => {
                reject(error);
            })
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}